# _*_ coding: utf8
'''
author: ning.luo
create_date: 2019/06/10
last_update: 2019/06/10
'''

import requests
import json
import random

class inquire_store_invoice_status():
    '''店铺发票状态查询接口'''

    def __init__(self):
        self.base_url = "http://test-api.intra.casstime.com/invoiceconfig-service"
        self.headers = {"Content-Type": "application/json", "charset": "UTF-8"}
        print("初始化条件！")

    def __del__(self):
        print("释放资源！")

    def test_set_invoiceconfig_enable(self):
        '''设置对项发票张数和金额'''
        url = self.base_url + "/invoiceconfig/enable"
        params = {
            "amount": 10000,
            "createdBy": "ning.luo",
            "createdDate": "2019-06-11T08:26:27.822Z",
            "enabled": 0,
            "id": 340000197812161860,
            "lastUpdateBy": "ning.luo",
            "lastUpdateDate": "2019-06-11T08:26:27.822Z",
            "number": 5,
            "productStoreId": "BS000001"
        }
        response = requests.post(url, json.dumps(params), headers=self.headers)
        try:
            assert response.status_code == 200
            print(response.text)
            print("用例1调用完成！")
        except Exception as e:
            print("状态码：", response.status_code)
            print("响应结果：", response.text)
            print("失败原因：", e)


    def test_check_status_invoice_amount(self):
        '''查询维修厂对应店铺能否开票及剩余张数'''
        url = self.base_url + "/invoiceconfig/iteminvoice/check"
        params = {
            "garageCompanyId": "kstest001",
            "storeItemInvoiceStatusDTOList":[
                {
                    "storeId": "GZZQ0001"
                }
            ]
        }
        response = requests.post(url, data=json.dumps(params), headers= self.headers)
        try:
            assert response.status_code == 200
            print(response.text)
            print("用例2调用完成！")
        except Exception as e:
            print("状态码：", response.status_code)
            print("响应结果：", response.text)
            print("失败原因：", e)

    def test_inventory_batchdeduct(self, **params):
        # 扣减发票张数和商品库存
        url = self.base_url + "/invoiceconfig/iteminvoice/inventory/batchdeduct"
        print("扣减参数：", params)
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        print("响应结果：", response.text)

    def test_inventory_batchdeduct_return(self, **params):
        # 回退发票张数和库存
        url = self.base_url + "/invoiceconfig/iteminvoice/inventory/rollback"
        print("回退参数：", params)
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        print("退货响应结果：", response.text)

    def get_orderId(self):
        orderId = 'S' + str(random.randint(10000000, 99999999))
        return orderId

    def get_requestNo(self):
        alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890'
        requestNo = ''.join(random.sample(alphabet, 10))
        return requestNo


if __name__ == "__main__":
    a = inquire_store_invoice_status()
    body_first_quote = {
            "requestNo": a.get_requestNo(),
            "userLoginId": "C0012838",
            "garageCompanyId": "12272",
            "orderInfoList": [{
                "productStoreId": "GZZQ0001",
                "orderId": a.get_orderId(),
                "amount": 1000,
                "itemInvoiceFlag": True,
                "orderItemInfoDTOS": [{
                    "productId": "15653499255367360059",
                    "checkInventory": True,
                    "quantity": 10
                    }
                ]
            }
        ]
    }
    body_second_quote = {
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "garageCompanyId": "12272",
        "orderInfoList": [{
            "productStoreId": "GZZQ0001",
            "orderId": a.get_orderId(),
            "amount": 1000,
            "itemInvoiceFlag": True,
            "orderItemInfoDTOS": [{
                "productId": "15653499255367360059",
                "checkInventory": False,
                "quantity": 10
            }
            ]
        }
        ]
    }
    body_invoice_false = {
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "garageCompanyId": "12272",
        "orderInfoList": [{
            "productStoreId": "GZZQ0001",
            "orderId": a.get_orderId(),
            "amount": 1000,
            "itemInvoiceFlag": False,
            "orderItemInfoDTOS": [{
                "productId": "15653499255367360059",
                "checkInventory": True,
                "quantity": 10
            }
            ]
        }
        ]
    }

    body_first_return = {
        "garageCompanyId": "12272",
        "rollbackType": "CANCELED",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S34591121",
            "amount": 10000,
            "productId": "15653499255367360059",
            "quantity": 10,
            "checkInventory": True
        }]
    }

    body_second_return = {
        "garageCompanyId": "12272",
        "rollbackType": "CANCELED",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S69869115",
            "amount": 10000,
            "productId": "15653499255367360059",
            "quantity": 10,
            "checkInventory": False
        }]
    }
    body_first_refund_one = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S45082405",
            "amount": 1000,
            "productId": "15653499255367360059",
            "quantity": 10,
            "checkInventory": True
        }]
    }
    body_first_refund_one_part = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S58336419",
            "amount": 500,
            "productId": "15653499255367360059",
            "quantity": 5,
            "checkInventory": True
        }]
    }
    body_second_refund_one = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S21309322",
            "amount": 1000,
            "productId": "15653499255367360059",
            "quantity": 10,
            "checkInventory": False
        }]
    }
    body_second_refund_one_part = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [{
            "itemInvoiceFlag": True,
            "productStoreId": "GZZQ0001",
            "orderId": "S80486140",
            "amount": 500,
            "productId": "15653499255367360059",
            "quantity": 5,
            "checkInventory": False
        }]
    }

    body_first_two = {
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "garageCompanyId": "12272",
        "orderInfoList": [{
            "productStoreId": "GZZQ0001",
            "orderId": a.get_orderId(),
            "amount": 1000,
            "itemInvoiceFlag": True,
            "orderItemInfoDTOS": [
                {
                "productId": "15653499255367360059",
                "checkInventory": False,
                "quantity": 10
                }
            ]},
            {
                "productStoreId": "BS000001",
                "orderId": a.get_orderId(),
                "amount": 1000,
                "itemInvoiceFlag": True,
                "orderItemInfoDTOS": [
                    {
                        "productId": "992838",
                        "checkInventory": False,
                        "quantity": 10
                    }
                ]
            }
        ]
    }
    body_first_two_canceled = {
        "garageCompanyId": "12272",
        "rollbackType": "CANCELED",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [
            {
                "itemInvoiceFlag": True,
                "productStoreId": "GZZQ0001",
                "orderId": "S43112250",
                "amount": 1000,
                "productId": "15653499255367360059",
                "quantity": 10,
                "checkInventory": True
            },
            {
                "itemInvoiceFlag": True,
                "productStoreId": "BS000001",
                "orderId": "S77630894",
                "amount": 1000,
                "productId": "992838",
                "quantity": 10,
                "checkInventory": True
            }
        ]
    }
    body_second_two_canceled = {
        "garageCompanyId": "12272",
        "rollbackType": "CANCELED",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [
            {
                "itemInvoiceFlag": True,
                "productStoreId": "GZZQ0001",
                "orderId": "S43112250",
                "amount": 1000,
                "productId": "15653499255367360059",
                "quantity": 10,
                "checkInventory": False
            },
            {
                "itemInvoiceFlag": True,
                "productStoreId": "BS000001",
                "orderId": "S77630894",
                "amount": 1000,
                "productId": "992838",
                "quantity": 10,
                "checkInventory": False
            }
        ]
    }
    body_first_two_refund = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [
            {
                "itemInvoiceFlag": True,
                "productStoreId": "GZZQ0001",
                "orderId": "S82246520",
                "amount": 500,
                "productId": "15653499255367360059",
                "quantity": 5,
                "checkInventory": True
            },
            {
                "itemInvoiceFlag": True,
                "productStoreId": "BS000001",
                "orderId": "S69755129",
                "amount": 500,
                "productId": "992838",
                "quantity": 5,
                "checkInventory": True
            }
        ]
    }
    body_second_two_refund = {
        "garageCompanyId": "12272",
        "rollbackType": "RETURN",
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "itemDTOList": [
            {
                "itemInvoiceFlag": True,
                "productStoreId": "GZZQ0001",
                "orderId": "S54986886",
                "amount": 500,
                "productId": "15653499255367360059",
                "quantity": 5,
                "checkInventory": False
            },
            {
                "itemInvoiceFlag": True,
                "productStoreId": "BS000001",
                "orderId": "S93320385",
                "amount": 500,
                "productId": "992838",
                "quantity": 5,
                "checkInventory": False
            }
        ]
    }

    body_f_s_quote = {
        "requestNo": a.get_requestNo(),
        "userLoginId": "C0012838",
        "garageCompanyId": "12272",
        "orderInfoList": [
            {
                "productStoreId": "GZZQ0001",
                "orderId": a.get_orderId(),
                "amount": 1000,
                "itemInvoiceFlag": True,
                "orderItemInfoDTOS": [
                    {
                        "productId": "15653499255367360059",
                        "checkInventory": True,
                        "quantity": 10
                    },
                    {
                        "productId": "15826869785151710001",
                        "checkInventory": False,
                        "quantity": 10
                    }
                ]
            },
            {
                "productStoreId": "BS000001",
                "orderId": a.get_orderId(),
                "amount": 1000,
                "itemInvoiceFlag": True,
                "orderItemInfoDTOS": [
                    {
                        "productId": "992838",
                        "checkInventory": True,
                        "quantity": 10
                    },
                    {
                        "productId": "Init51700165",
                        "checkInventory": False,
                        "quantity": 10
                    }
                ]
            }
        ]
    }
    body_f_s_quote_canceled = {
        'garageCompanyId': '12272',
        'rollbackType': 'CANCELED',
        'requestNo': '3t0qp5vwi8',
        'userLoginId': 'C0012838',
        'itemDTOList': [{
                'itemInvoiceFlag': True,
                'productStoreId': 'GZZQ0001',
                'orderId': 'S34395719',
                'amount': 1000,
                'productId': '15653499255367360059',
                'quantity': 10,
                'checkInventory': True
            }, {
                'itemInvoiceFlag': True,
                'productStoreId': 'GZZQ0001',
                'orderId': 'S34395719',
                'amount': 1000,
                'productId': '15826869785151710001',
                'quantity': 10,
                'checkInventory': False
            }, {
                'itemInvoiceFlag': True,
                'productStoreId': 'BS000001',
                'orderId': 'S20642128',
                'amount': 1000,
                'productId': '992838',
                'quantity': 10,
                'checkInventory': True
            }, {
                'itemInvoiceFlag': True,
                'productStoreId': 'BS000001',
                'orderId': 'S20642128',
                'amount': 1000,
                'productId': 'Init51700165',
                'quantity': 10,
                'checkInventory': False
            }
        ]
    }

    # a.test_set_invoiceconfig_enable()
    # a.test_check_status_invoice_amount()

    # 库存扣减
    a.test_inventory_batchdeduct(**body_f_s_quote)

    # 库存回退
    # a.test_inventory_batchdeduct_return(**body_f_s_quote_canceled)
